#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import yaml
from pathlib import Path

class AttrDict(dict):
    def __getattribute__(self, item):
        r = self[item]
        if isinstance(r, dict):
            return AttrDict(r)
        else:
            return r


class Readyaml():
    def __init__(self,path):
        '''参数是目录就遍历读取所有文件内容，组合为一个字典'''
        p = Path(path)
        cfg = dict()
        if p.is_dir():
            yamls=p.iterdir()
            for y in yamls:
                name = y.name.split('.')[0]
                with y.open(encoding='utf8') as fo:
                    cfg[name] = yaml.safe_load(fo)
        else:
            with p.open(encoding='utf8') as fo:
                cfg = yaml.safe_load(fo)
        self.cfg = AttrDict(cfg)

    def __getattribute__(self, item):
        '''添加属性取值的方式'''
        if item=='cfg':
            return super().__getattribute__(item)
        else:
            r=self.cfg[item]
            if isinstance(r,dict):
                return AttrDict(r)
            else:
                return r

    def __getitem__(self, key):
        return self.cfg[key]

if __name__ == '__main__':
    r=Readyaml(r'D:\workspace\PycharmProjects\web-demotest\page_element')
    print(r.login.usernameBox.loc)
